package org.spring.springboot.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.spring.springboot.entity.IntervieweeManage;
import org.spring.springboot.result.R;
import org.spring.springboot.result.Result;
import org.spring.springboot.server.IntervieweeManageService;
import org.spring.springboot.util.ConstantUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "被访者管理")
@RequestMapping("/intervieweeManage")
public class IntervieweeManageController {
    @Autowired
    private IntervieweeManageService intervieweeManageService;

    @ApiOperation(value = "新增被访者")
    @PostMapping("/addUser")
    public Result addUser(@RequestBody IntervieweeManage im){
        intervieweeManageService.addUser(im);
        return Result.newSuccessResult(im);
    }

    @ApiOperation(value = "查询被访者姓名")
    @PostMapping("/userNameList")
    public R userNameList(Integer pageNum,Integer pageSize) {
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<String> list = intervieweeManageService.getUserNameList();
        Long total = page.getTotal();
        return R.newSuccessResult(list,total);
    }

    @ApiOperation(value = "更新被访者")
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody IntervieweeManage im){
        intervieweeManageService.updateUser(im);
        return Result.newSuccessResult(im);
    }

    @ApiOperation(value = "查询所有被访者信息")
    @PostMapping("/userList")
    public R userList(Integer pageNum,Integer pageSize) {
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> list = intervieweeManageService.getUserList();
        Long total = page.getTotal();
        return R.newSuccessResult(list,total);
    }

    @ApiOperation(value = "删除被访者")
    @PostMapping("/deleteUser")
    public Result deleteUser(@RequestParam(value = "id") Integer id){
        intervieweeManageService.deleteUser(id);
        return Result.newSuccessResult("id:"+id+" 被访者已删除");
    }
}
